//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Modules;
using NopSolutions.NopCommerce.Controls;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ProductVariantAttributeValuesControl : BaseNopAdministrationUserControl
    {
        private void BindData() {
            ProductVariantAttribute productVariantAttribute = this.ProductAttributeService.GetProductVariantAttributeById(this.ProductVariantAttributeId);
            if (productVariantAttribute != null) {
                if (this.HasLocalizableContent) {
                    var languages = this.GetLocalizableLanguagesSupported();
                    rptrLanguageTabs.DataSource = languages;
                    rptrLanguageTabs.DataBind();
                    rptrLanguageDivs.DataSource = languages;
                    rptrLanguageDivs.DataBind();
                }

                ProductVariant productVariant = productVariantAttribute.ProductVariant;
                if (productVariant == null)
                    Response.Redirect("Products.aspx");
                ProductAttribute productAttribute = productVariantAttribute.ProductAttribute;
                if (productAttribute == null)
                    Response.Redirect("Products.aspx");

                this.lblTitle.Text = string.Format(GetLocaleResourceString("Admin.ProductVariantAttributeValues.AddEdit"), Server.HtmlEncode(productAttribute.Name), Server.HtmlEncode(productVariant.FullProductName));
                this.hlProductURL.NavigateUrl = CommonHelper.GetStoreAdminLocation() + "ProductVariantDetails.aspx?ProductVariantID=" + productVariant.ProductVariantId;

                var productVariantAttributeValues = this.ProductAttributeService.GetProductVariantAttributeValues(productVariantAttribute.ProductVariantAttributeId);
                if (productVariantAttributeValues.Count > 0) {
                    this.gvProductVariantAttributeValues.Visible = true;
                    this.gvProductVariantAttributeValues.DataSource = productVariantAttributeValues;
                    this.gvProductVariantAttributeValues.DataBind();
                }
                else
                    this.gvProductVariantAttributeValues.Visible = false;
            }
            else
                Response.Redirect("Products.aspx");
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (!IsPostBack) {
                gvProductVariantAttributeValues.Columns[1].HeaderText = string.Format("{0} [{1}]", GetLocaleResourceString("Admin.ProductVariantAttributeValues.PriceAdjustment"), this.CurrencyService.PrimaryStoreCurrency.CurrencyCode);
                this.BindData();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e) {
            try {
                ProductVariantAttribute productVariantAttribute = this.ProductAttributeService.GetProductVariantAttributeById(this.ProductVariantAttributeId);
                if (productVariantAttribute != null) {
                    var productVariantAttributeValue = new ProductVariantAttributeValue() {
                        ProductVariantAttributeId = productVariantAttribute.ProductVariantAttributeId,
                        Name = txtNewName.Text,
                        PriceAdjustment = txtNewPriceAdjustment.Value,
                        WeightAdjustment = txtNewWeightAdjustment.Value,
                        IsPreSelected = cbNewIsPreSelected.Checked,
                        DisplayOrder = txtNewDisplayOrder.Value,
                        DisplayAdjustment = chkNewDisplayAdjustment.Checked
                    };

                    this.ProductAttributeService.InsertProductVariantAttributeValue(productVariantAttributeValue);

                    SaveLocalizableContent(productVariantAttributeValue);

                    BindData();

                    txtNewName.Text = string.Empty;
                    txtNewPriceAdjustment.Value = 0;
                    txtNewWeightAdjustment.Value = 0;
                    txtNewDisplayOrder.Value = 1;
                }
            }
            catch (Exception exc) {
                ProcessException(exc);
            }
        }

        protected void SaveLocalizableContent(ProductVariantAttributeValue pvav) {
            if (pvav == null)
                return;

            if (!this.HasLocalizableContent)
                return;

            foreach (RepeaterItem item in rptrLanguageDivs.Items) {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) {
                    var txtNewLocalizedName = (TextBox)item.FindControl("txtNewLocalizedName");
                    var lblLanguageId = (Label)item.FindControl("lblLanguageId");

                    int languageId = int.Parse(lblLanguageId.Text);
                    string name = txtNewLocalizedName.Text;

                    bool allFieldsAreEmpty = string.IsNullOrEmpty(name);

                    var content = this.ProductAttributeService.GetProductVariantAttributeValueLocalizedByProductVariantAttributeValueIdAndLanguageId(pvav.ProductVariantAttributeValueId, languageId);
                    if (content == null) {
                        if (!allFieldsAreEmpty && languageId > 0) {
                            //only insert if one of the fields are filled out (avoid too many empty records in db...)
                            content = new ProductVariantAttributeValueLocalized() {
                                ProductVariantAttributeValueId = pvav.ProductVariantAttributeValueId,
                                LanguageId = languageId,
                                Name = name
                            };
                            this.ProductAttributeService.InsertProductVariantAttributeValueLocalized(content);
                        }
                    }
                    else {
                        if (languageId > 0) {
                            content.LanguageId = languageId;
                            content.Name = name;
                            this.ProductAttributeService.UpdateProductVariantAttributeValueLocalized(content);
                        }
                    }
                }
            }
        }

        protected void SaveLocalizableContentGrid(ProductVariantAttributeValue pvav) {
            if (pvav == null)
                return;

            if (!this.HasLocalizableContent)
                return;

            foreach (GridViewRow row in gvProductVariantAttributeValues.Rows) {
                Repeater rptrLanguageDivs2 = row.FindControl("rptrLanguageDivs2") as Repeater;
                if (rptrLanguageDivs2 != null) {
                    HiddenField hfProductVariantAttributeValueId = row.FindControl("hfProductVariantAttributeValueId") as HiddenField;
                    int pvavId = int.Parse(hfProductVariantAttributeValueId.Value);
                    if (pvavId == pvav.ProductVariantAttributeValueId) {
                        foreach (RepeaterItem item in rptrLanguageDivs2.Items) {
                            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) {
                                var txtLocalizedName = (TextBox)item.FindControl("txtLocalizedName");
                                var lblLanguageId = (Label)item.FindControl("lblLanguageId");

                                int languageId = int.Parse(lblLanguageId.Text);
                                string name = txtLocalizedName.Text;

                                bool allFieldsAreEmpty = string.IsNullOrEmpty(name);

                                var content = this.ProductAttributeService.GetProductVariantAttributeValueLocalizedByProductVariantAttributeValueIdAndLanguageId(pvav.ProductVariantAttributeValueId, languageId);
                                if (content == null) {
                                    if (!allFieldsAreEmpty && languageId > 0) {
                                        //only insert if one of the fields are filled out (avoid too many empty records in db...)
                                        content = new ProductVariantAttributeValueLocalized() {
                                            ProductVariantAttributeValueId = pvav.ProductVariantAttributeValueId,
                                            LanguageId = languageId,
                                            Name = name
                                        };
                                        this.ProductAttributeService.InsertProductVariantAttributeValueLocalized(content);
                                    }
                                }
                                else {
                                    if (languageId > 0) {
                                        content.LanguageId = languageId;
                                        content.Name = name;
                                        this.ProductAttributeService.UpdateProductVariantAttributeValueLocalized(content);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void rptrLanguageDivs_ItemDataBound(object sender, RepeaterItemEventArgs e) {

        }

        protected void gvProductVariantAttributeValues_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "UpdateProductVariantAttributeValue") {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvProductVariantAttributeValues.Rows[index];

                HiddenField hfProductVariantAttributeValueId = row.FindControl("hfProductVariantAttributeValueId") as HiddenField;
                var txtName = row.FindControl("txtName") as SimpleTextBox;
                DecimalTextBox txtPriceAdjustment = row.FindControl("txtPriceAdjustment") as DecimalTextBox;
                DecimalTextBox txtWeightAdjustment = row.FindControl("txtWeightAdjustment") as DecimalTextBox;
                CheckBox cbIsPreSelected = row.FindControl("cbIsPreSelected") as CheckBox;
                CheckBox chkDisplayAdjustment = row.FindControl("chkDisplayAdjustment") as CheckBox;
                NumericTextBox txtDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;

                int productVariantAttributeValueId = int.Parse(hfProductVariantAttributeValueId.Value);
                string name = txtName.Text;
                decimal priceAdjustment = txtPriceAdjustment.Value;
                decimal weightAdjustment = txtWeightAdjustment.Value;
                bool isPreSelected = cbIsPreSelected.Checked;
                bool displayAdjustment = chkDisplayAdjustment.Checked;
                int displayOrder = txtDisplayOrder.Value;

                ProductVariantAttributeValue productVariantAttributeValue = this.ProductAttributeService.GetProductVariantAttributeValueById(productVariantAttributeValueId);

                if (productVariantAttributeValue != null) {
                    productVariantAttributeValue.Name = name;
                    productVariantAttributeValue.PriceAdjustment = priceAdjustment;
                    productVariantAttributeValue.WeightAdjustment = weightAdjustment;
                    productVariantAttributeValue.IsPreSelected = isPreSelected;
                    productVariantAttributeValue.DisplayAdjustment = displayAdjustment;
                    productVariantAttributeValue.DisplayOrder = displayOrder;
                    this.ProductAttributeService.UpdateProductVariantAttributeValue(productVariantAttributeValue);

                    SaveLocalizableContentGrid(productVariantAttributeValue);
                }
                BindData();
            }
        }

        protected void gvProductVariantAttributeValues_RowDeleting(object sender, GridViewDeleteEventArgs e) {
            int productVariantAttributeValueId = (int)gvProductVariantAttributeValues.DataKeys[e.RowIndex]["ProductVariantAttributeValueId"];
            this.ProductAttributeService.DeleteProductVariantAttributeValue(productVariantAttributeValueId);
            BindData();
        }

        protected void gvProductVariantAttributeValues_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                ProductVariantAttributeValue productVariantAttributeValue = (ProductVariantAttributeValue)e.Row.DataItem;

                Button btnUpdate = e.Row.FindControl("btnUpdate") as Button;
                if (btnUpdate != null)
                    btnUpdate.CommandArgument = e.Row.RowIndex.ToString();

                Repeater rptrLanguageDivs2 = e.Row.FindControl("rptrLanguageDivs2") as Repeater;
                if (rptrLanguageDivs2 != null) {
                    if (this.HasLocalizableContent) {
                        var languages = this.GetLocalizableLanguagesSupported();
                        rptrLanguageDivs2.DataSource = languages;
                        rptrLanguageDivs2.DataBind();
                    }
                }
            }
        }

        protected void rptrLanguageDivs2_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                var txtLocalizedName = (TextBox)e.Item.FindControl("txtLocalizedName");
                var lblLanguageId = (Label)e.Item.FindControl("lblLanguageId");
                var hfProductVariantAttributeValueId = (HiddenField)e.Item.Parent.Parent.FindControl("hfProductVariantAttributeValueId");

                int languageId = int.Parse(lblLanguageId.Text);
                int pvavId = Convert.ToInt32(hfProductVariantAttributeValueId.Value);
                ProductVariantAttributeValue pvav = this.ProductAttributeService.GetProductVariantAttributeValueById(pvavId);
                if (pvav != null) {
                    var content = this.ProductAttributeService.GetProductVariantAttributeValueLocalizedByProductVariantAttributeValueIdAndLanguageId(pvavId, languageId);
                    if (content != null) {
                        txtLocalizedName.Text = content.Name;
                    }
                }
            }
        }

        public int ProductVariantAttributeId {
            get {
                return CommonHelper.QueryStringInt("ProductVariantAttributeId");
            }
        }
    }
}